package org.dromara.system.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.AppSupplierDetailVo;
import org.dromara.system.domain.bo.AppSupplierDetailBo;
import org.dromara.system.service.IAppSupplierDetailService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 供应商管理
 *
 * @author Lion Li
 * @date 2025-09-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/supplierDetail")
public class AppSupplierDetailController extends BaseController {

    private final IAppSupplierDetailService appSupplierDetailService;


    /**
     * 根据公司id查询供应商列表
     */
    @SaCheckPermission("system:supplierDetail:list")
    @GetMapping("/listByCompanyId")
    public R<List<AppSupplierDetailVo>> listByCompanyId(String companyId) {
        if (StrUtil.isBlank(companyId)){
            return R.fail("公司id不能为空");
        }
        return appSupplierDetailService.listByCompanyId(companyId);
    }


    /**
     * 查询供应商列表
     */
    @SaCheckPermission("system:supplierDetail:list")
    @GetMapping("/list")
    public TableDataInfo<AppSupplierDetailVo> list(AppSupplierDetailBo bo, PageQuery pageQuery) {
        return appSupplierDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出供应商详情列表
     */
    @SaCheckPermission("system:supplierDetail:export")
    @Log(title = "供应商详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppSupplierDetailBo bo, HttpServletResponse response) {
        List<AppSupplierDetailVo> list = appSupplierDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "供应商详情", AppSupplierDetailVo.class, response);
    }

    /**
     * 根据主键id获取供应商详情详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:supplierDetail:query")
    @GetMapping("/{id}")
    public R<AppSupplierDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(appSupplierDetailService.queryById(id));
    }

    /**
     * 根据手机号获取供应商详细信息
     *
     * @param phone 手机号
     */
    @SaCheckPermission("system:supplierDetail:query")
    @GetMapping("/getInfoByPhone")
    @SaIgnore
    public R<AppSupplierDetailVo> getInfoByPhone(String phone) {
        return R.ok(appSupplierDetailService.getInfoByPhone(phone));
    }


    /**
     * 供应商申请
     */
    @SaCheckPermission("system:supplierDetail:add")
    @Log(title = "供应商申请", businessType = BusinessType.INSERT)
    @SaIgnore
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppSupplierDetailBo bo) {
        return toAjax(appSupplierDetailService.insertByBo(bo));
    }

    /**
     * 修改供应商详情
     */
    @SaCheckPermission("system:supplierDetail:edit")
    @Log(title = "供应商详情", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @SaIgnore
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppSupplierDetailBo bo) {
        return toAjax(appSupplierDetailService.updateByBo(bo));
    }

    /**
     * 删除供应商详情
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:supplierDetail:remove")
    @Log(title = "供应商详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appSupplierDetailService.deleteWithValidByIds(List.of(ids), true));
    }
}
